﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.Framework.Common.Enums;

namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class ReturnVisitManageView : BaseUserControl
    {
        private DataSet dsVisit;
        public ReturnVisitManageView()
        {
            InitializeComponent();
            Init();

            ButtonItemIconHelper.InitRibbonBar(this.rbMenu);

            //this.returnVisitSearchControl1.ReturnVisitSearch = this.CustomerReturnVisitSearch;
        }

        public ReturnVisitManageView(ClientAppBase _app)
            : base(_app)
        {
            InitializeComponent();
            Init();

            ButtonItemIconHelper.InitRibbonBar(this.rbMenu);

            //this.returnVisitSearchControl1.ReturnVisitSearch = this.CustomerReturnVisitSearch;
        }

        private void CustomerReturnVisitSearch(string sFilter)
        {
            if (gdReturnVisit.DataSource != null)
            {
                DataView dv = (DataView)gdReturnVisit.DataSource;
                dv.RowFilter = sFilter;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmReturnVisitAdd form = new frmReturnVisitAdd();
            form.Mode = ControlClassModeEnum.emInsert;
            if (form.ShowDialog() == DialogResult.OK)
            {
                Init();
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            int iRowHandle = gvReturnVisit.FocusedRowHandle;
            if (iRowHandle < 0)
            {
                MsgBox.MsgInfo("请选择要修改的回访信息。");
                return;
            }

            DataRow dr = gvReturnVisit.GetDataRow(iRowHandle);
            DBEntityCustomerReturnVisit visitInfo = new DBEntityCustomerReturnVisit(dr);
            frmReturnVisitAdd form = new frmReturnVisitAdd();
            form.Mode = ControlClassModeEnum.emEdit;
            form.CustomerReturnVisitInfo = visitInfo;
            if (form.ShowDialog() == DialogResult.OK)
            {
                Init();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int iRowHandle = gvReturnVisit.FocusedRowHandle;
            if (iRowHandle < 0)
            {
                MsgBox.MsgInfo("请选择要删除的回访信息。");
                return;
            }

            bool bFlag = MsgBox.MsgQue("确定要删除此回访信息吗？");
            if (bFlag == false)
                return;
            
            DataRow dr = gvReturnVisit.GetDataRow(iRowHandle);
            int iReturnVisitID = Convert.ToInt32(dr[DBEntityCustomerReturnVisit.FieldName.CRETURNVISITID]);
            string error = string.Empty;
            bFlag = BussinessProxyManager.Instance.DeleteCustomerReturnVisit(iReturnVisitID, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("删除回访信息失败\r\n" + error);
                return;
            }
            else
            {
                Init();
            }
        }

        private void Init()
        {
            string error = string.Empty;
            string sFilter = string.Empty;
            dsVisit = new DataSet();
            bool bFlag = BussinessProxyManager.Instance.QueryCustomerReturnVisit(sFilter, out dsVisit, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("获取回访信息失败\r\n" + error);
                return;
            }

            gdReturnVisit.DataSource = null;
            gdReturnVisit.DataSource = dsVisit.Tables[0].DefaultView;
        }

        private void gvReturnVisit_DoubleClick(object sender, EventArgs e)
        {
            btnModify_Click(null, null);
        }
    }
}
